package com.dgut.usermodule.web;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dgut.common.util.ExtAjaxResponse;
import com.dgut.common.util.ExtPageable;
import com.dgut.usermodule.entity.User;
import com.dgut.usermodule.entity.UserListDTO;
import com.dgut.usermodule.entity.UserSaveDTO;
import com.dgut.usermodule.entity.UserUpdateDTO;
import com.dgut.usermodule.service.UserService;

@RestController
@RequestMapping("/user")
public class UserCtrl {
	@Autowired
	private UserService service;
	/**
	 *模糊查询用户列表
	 * @return
	 */
	@RequestMapping("/list")
	public List<User> list(){
		return service.findAll();
	}
	/**
	 * 分页查询，模糊查询
	 */
	@RequestMapping("/findall")
	public Page<User> findAll(UserListDTO dto,ExtPageable extPageable){
		return service.findPage(UserListDTO.getSpecification(dto),extPageable.getPageable());
	}
	
	/**
	 * 根据id查询一个用户
	 */
	@RequestMapping("/findOne")
	public User findONe(@RequestParam Long id) {
		return service.findOne(id);
	}
	/**
	 * 根据用户名查询用户信息
	 */
	@RequestMapping("/findbyname")
	public User findByName() {
		return service.findByName((String)SecurityUtils.getSubject().getPrincipal());
	}
	/**
	 * 传入用户信息，添加用户
	 */
	@RequestMapping("/save")
	public String save(@Validated UserSaveDTO dto,BindingResult result) {
		 String res;
		 if (result.hasErrors()) {  
	            List<ObjectError> errors = result.getAllErrors();
	            return errors.get(0).getDefaultMessage();
		 }else {
			 User user = new User();
			 user.setEmail(dto.getEmail());
			 user.setUserName(dto.getUserName());
			 user.setPassword(dto.getPassword());
			 user.setPhone(dto.getPhone());
			 res = service.save(user);
		}
		return res;
	}
	/**
	 * 传入用户信息，更新用户
	 */
	@RequestMapping("/update")
	public ExtAjaxResponse update(@Validated UserUpdateDTO dto, BindingResult result) {
		String res;
		 if (result.hasErrors()) {  
	            List<ObjectError> errors = result.getAllErrors();
	            return new ExtAjaxResponse(false,errors.get(0).getDefaultMessage());
		 }else {
			 User user = new User();
			 user.setId(dto.getId());
			 user.setEmail(dto.getEmail());
			 user.setUserName(dto.getUserName());
			 user.setPassword(dto.getPassword());
			 user.setPhone(dto.getPhone());
			 res = service.update(user);
		}
		 if (res=="true") {
			 return new ExtAjaxResponse(true, "操作成功！");
		}
		return new ExtAjaxResponse(false, "操作失败！");
	}
	@RequestMapping("/delete")
	public ExtAjaxResponse delete(@RequestParam Long id) {
		if (service.delete(id)) {
			return new ExtAjaxResponse(true, "操作成功！");
		}
		return new ExtAjaxResponse(false, "操作失败！");
	}
}
